package com.example.demo.controller.system;

import com.example.demo.model.ResponseMessage;
import com.example.demo.model.system.User;
import com.example.demo.service.system.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService service;
    @RequestMapping("save")
    public ResponseMessage save(User user){
        ResponseMessage responseMessage = new ResponseMessage();
        Boolean flag = service.save(user);
        responseMessage.setFlag(flag);

        return  responseMessage;
    }

    @RequestMapping("selectAll")
    @ResponseBody
    public List<User> selectAll(){ return service.selectAll(); }

    @RequestMapping("findUser")
    @ResponseBody
    public List<User> findUser(String username,Integer role_id){ return service.findUser(username,role_id);}

    @RequestMapping("addUser")
    public Integer addUser(User user){return service.addUser(user);}

    @RequestMapping("deleteById")
    public Boolean deleteById(Integer userId){return service.deleteById(userId)==1;}

    @RequestMapping("updateById")
    public Integer updateById(User user){return service.updateById(user);}

    @RequestMapping("login")
    public Boolean login(String username,String password) {
        Boolean userlogin = service.login(username, password);
        if (userlogin != null) {
            return true;
        } else {
            return false;
        }
    }
}
